package com.skt.common_hr.congmoney_sac.model
{
	import com.skt.common_hr.common.approval.IRequestApprovalProxy;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.congmoney_sac.model.vo.CongMoneyWriteVO;

	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;

	public class CelebrationApprovalProxy extends CommonProxy implements IRequestApprovalProxy
	{
		public static const NAME:String = "CelebrationApprovalProxy";

		public var dataProvider:CongMoneyWriteVO;

		private var templateHTML:String = "";
		/**
		 * 생성자
		 *
		 * @param
		 * @return void
		 */
		public function CelebrationApprovalProxy(data:Object=null)
		{
			super(NAME, data);
			loadTemplate();
		}

		private var _key:String = "";
		public function get key():String
		{
			return _key;
		}
		public function set key(k:String):void
		{
			_key = k;
		}


		public function get title():String
		{
			var tmp:String = "경조사 신청";
			tmp += " (" + dataProvider.ZTEXT + ")"
			return tmp;
		}

		public function get content():String
		{
			return buildHTML(dataProvider);
		}

		public function get appCode():String
		{
			return "06"; 	//
		}


 		private function loadTemplate():void
		{
			var request:URLRequest = new URLRequest("assets/template/celebration_sac.tmp");
			loader.addEventListener(Event.COMPLETE, templateResult);
			loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

            try {
                loader.load(request);
            } catch (error:Error) {
                trace("Unable to load requested document.");
            }
		}

		private function templateResult(event:Event):void
		{
			templateHTML = loader.data;
		}

		private function ioErrorHandler(event:IOErrorEvent):void
		{
			trace("ioErrorHandler: " + event);
		}

 		private function buildHTML(vo:CongMoneyWriteVO):String
		{
			var htmlAll:String = templateHTML;
			var htmlBlock:String = "";

			var ptnORGTX	:RegExp  = /__ORGTX__/;
			var ptnCPERNR	:RegExp  = /__CPERNR__/;
			var ptnCNAME	:RegExp  = /__CNAME__/;
			var ptnCOPER	:RegExp  = /__COPER__/;
			var ptnCOREG	:RegExp  = /__COREG__/;
			var ptnZTEXT	:RegExp  = /__ZTEXT__/;
			var ptnFTEXT	:RegExp = /__FTEXT__/;
			var ptnCOSTEN	:RegExp = /__COSTEN__/;			
			var ptnBANKA	:RegExp = /__BANKA__/;
			var ptnBANKN	:RegExp = /__BANKN__/;
			var ptnCBETRG	:RegExp = /__CBETRG__/;
			
			//var ptnZFNR	:RegExp = /__ZFNR_ZWRTH__/;
			

			htmlAll = htmlAll.replace(ptnORGTX	, vo.ORGTX);
			htmlAll = htmlAll.replace(ptnCPERNR	, vo.CPERNR);
			htmlAll = htmlAll.replace(ptnCNAME	, vo.CNAME);
			htmlAll = htmlAll.replace(ptnCOPER	, vo.COPER);
			htmlAll = htmlAll.replace(ptnCOREG	, vo.COREG);
			htmlAll = htmlAll.replace(ptnZTEXT	, vo.ZTEXT);
			htmlAll = htmlAll.replace(ptnFTEXT	, vo.FTEXT);			
			htmlAll = htmlAll.replace(ptnBANKA	, vo.BANKA);
			htmlAll = htmlAll.replace(ptnBANKN	, vo.BANKN);
			htmlAll = htmlAll.replace(ptnCBETRG	, vo.CBETRG);
		/* 	
			if(vo.ZFNR=='X' || vo.ZWRTH=='X') {
				htmlAll = htmlAll.replace(ptnZFNR	, '화환/경조물품');	
			} else {
				htmlAll = htmlAll.replace(ptnZFNR	, ' ');
			}
			 */

			//htmlAll = htmlAll.replace(ptnCOSTEN, vo.COSTEN);

			if(vo.COSEL == 'X') {
				htmlAll = htmlAll.replace(ptnCOSTEN, vo.COSTEN);
			} else {
				htmlAll = htmlAll.replace(ptnCOSTEN, '');
			}
			
			return htmlAll;
		}
	}
}